package com.xiaode.controller.callBack;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xiaode.common.constant.Const;
import com.xiaode.common.model.Admin;
import com.xiaode.common.model.Student;
import com.xiaode.common.model.UserAccount;
import com.xiaode.common.model.WlDepartment;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class ReturnService {

	
	
	
	/**
	 * 编辑学生
	 */
	@Before(Tx.class)
	public void editStudent(JSONObject jsonuser) throws Exception {
		String userId = jsonuser.getStr("userId");
		UserAccount ua = UserAccount.dao.findFirst("SELECT * FROM user_account WHERE eduID = ?", userId);
		List<String> dc = Db.query("SELECT dept_code FROM wl_department");
		if(BeanUtil.isNotEmpty(ua)) {
			Student s = Student.dao.findFirst("SELECT * FROM student WHERE accountId = ?", ua.getId());
//			if(StrUtil.isNotBlank(jsonuser.getStr("avatar"))) {
//				s.setAvatarUrl(jsonuser.getStr("avatar"));
//			}
			s.setStudentNo(jsonuser.getStr("employeeId"));
			s.setName(jsonuser.getStr("userNameCn"));
			s.setSex(StrUtil.equals(jsonuser.getStr("sex"), "F")?2:1);
			s.setMobile(jsonuser.getStr("mobileNumber"));
			String gradeStr = jsonuser.getJSONObject("extAttr").getStr("年级组").split(";")[0];
			if(StrUtil.equals(gradeStr, "高一年级")) {
				s.setGrade(5);
			}
			if(StrUtil.equals(gradeStr, "高二年级")) {
				s.setGrade(1);
			}
			if(StrUtil.equals(gradeStr, "高三年级")) {
				s.setGrade(2);
			}
			s.setUpdateTime(LocalDateTime.now());
			String number = jsonuser.getJSONObject("extAttr").getStr("学籍号-学生");
			if(StrUtil.isNotBlank(number)) {
				s.setNumber(number);
			}
			List<String> deptCodes = JSONUtil.toList(jsonuser.getStr("deptCodes"), String.class) ;
			Set<String> dept = CollUtil.intersectionDistinct(deptCodes, dc);
			if(!dept.isEmpty()) {
				String deptCode = dept.iterator().next();
				WlDepartment wd = WlDepartment.dao.findFirst("SELECT * FROM wl_department WHERE dept_code = ?", deptCode);
				s.setClasId(wd.getOrgClassId());
			}
			s.update();
		}else {
			String account = jsonuser.getJSONObject("extAttr").getStr("学籍号-学生");
			ua = new UserAccount();
			ua.setAccount(account);
			ua.setAddTime(LocalDateTime.now());
			ua.setUserType(2);
			ua.setIsBindingPedestal(true);
			ua.setEduID(userId);
			ua.save();
			ua.setPassword(SecureUtil.md5(SecureUtil.md5("111111") + ua.getId() + Const.salt));
			ua.update();
			
			Student s = new Student();
//			if(StrUtil.isNotBlank(jsonuser.getStr("avatar"))) {
//				s.setAvatarUrl(jsonuser.getStr("avatar"));
//			}
			s.setStudentNo(jsonuser.getStr("corpUserId"));
			s.setName(jsonuser.getStr("userNameCn"));
			s.setSex(StrUtil.equals(jsonuser.getStr("sex"), "F")?2:1);
			s.setMobile(jsonuser.getStr("mobileNumber"));
			String gradeStr = jsonuser.getJSONObject("extAttr").getStr("年级组").split(";")[0];
			if(StrUtil.equals(gradeStr, "高一年级")) {
				s.setGrade(5);
			}
			if(StrUtil.equals(gradeStr, "高二年级")) {
				s.setGrade(1);
			}
			if(StrUtil.equals(gradeStr, "高三年级")) {
				s.setGrade(2);
			}
			s.setUpdateTime(LocalDateTime.now());
			String number = jsonuser.getJSONObject("extAttr").getStr("学籍号-学生");
			s.setNumber(number);
			List<String> deptCodes = JSONUtil.toList(jsonuser.getStr("deptCodes"), String.class) ;
			Set<String> dept = CollUtil.intersectionDistinct(deptCodes, dc);
			if(!dept.isEmpty()) {
				String deptCode = dept.iterator().next();
				WlDepartment wd = WlDepartment.dao.findFirst("SELECT * FROM wl_department WHERE dept_code = ?", deptCode);
				s.setClasId(wd.getOrgClassId());
			}
			s.setAddTime(LocalDateTime.now());
			s.setAccountId(ua.getId());
			s.setUserType(2);
			s.save();
		}
	}
//	1753,1780,1790,1815,1836,1838,1851,1872,1874,1875,1876,1877,1878,1879,1880,1881,1882,1883,1884,1885,1886,1888
//	"陈红","王俊伟","孔怀儒","徐利红","赵良","王辉","费薇","郭佳豪","孙梅(已退休)","练依溟","刘小杰","刘哲","孙安娜","孙亦仁","王娜","谢旌","徐峰","徐勤华","许坚",培培,测试
	/**
	 * 删除用户
	 */
	@Before(Tx.class)
	public void delUser(String userCode) throws Exception {
		UserAccount au = UserAccount.dao.findFirst("SELECT * FROM user_account WHERE eduID = ?", userCode);
		Integer userType = au.getUserType();
		if(BeanUtil.isNotEmpty(au)) {
			au.setState(2);
			au.update();
			if(userType == Const.UserType.admin) {
				Admin admin = Admin.dao.findFirst("SELECT id FROM admin WHERE accountId = ?", au.getId());
				admin.setState(2);
				admin.update();
			}
			if(userType == Const.UserType.student) {
				Student student = Student.dao.findFirst("SELECT id FROM student WHERE accountId = ?", au.getId());
				student.setState(2);
				student.update();
			}
		}
	}


	/**
	 * 编辑老师
	 */
	@Before(Tx.class)
	public void editTeacher(JSONObject jsonuser) throws Exception {
		String userId = jsonuser.getStr("userId");
		UserAccount ua = UserAccount.dao.findFirst("SELECT * FROM user_account WHERE eduID = ?", userId);
		if(BeanUtil.isNotEmpty(ua)) {
			Admin a = Admin.dao.findFirst("SELECT * FROM admin WHERE accountId = ?", ua.getAccount());
			if(StrUtil.isNotBlank(jsonuser.getStr("employeeId"))){
				a.setJobNum(jsonuser.getStr("employeeId"));
			}
			a.setName(jsonuser.getStr("userNameCn"));
			a.setGender(StrUtil.equals(jsonuser.getStr("sex"), "F")?2:1);
			a.setPhone(jsonuser.getStr("mobileNumber"));
			a.setUpdateTime(LocalDateTime.now());
			a.setEmail(jsonuser.getStr("userEmail"));
			a.setSubjects(jsonuser.getStr("position"));
//			if(StrUtil.isNotBlank(jsonuser.getStr("avatar"))) {
//				a.setAvatarUrl(jsonuser.getStr("avatar"));
//			}
			if(StrUtil.isNotBlank(jsonuser.getStr("userEmail"))) {
				a.setEmail(jsonuser.getStr("userEmail"));
			}
			a.update();
			Boolean b = jsonuser.getInt("isAdmin")==0?false:true;
			if(ua.getCanLoginAdmin() != b) {
				ua.setCanLoginAdmin(b);
				ua.update();
			}
		}else {
			String account = jsonuser.getStr("mobileNumber").split("-")[1];//手机号作为账号
			ua = new UserAccount();
			ua.setAccount(account);
			ua.setAddTime(LocalDateTime.now());
			ua.setUserType(1);
			ua.setIsBindingPedestal(true);
			ua.setEduID(userId);
			ua.save();
			ua.setPassword(SecureUtil.md5(SecureUtil.md5("111111") + ua.getId() + Const.salt));
			ua.update();
			
			Admin a = new Admin();
			a.setPhone(account);
//			if(StrUtil.isNotBlank(jsonuser.getStr("avatar"))) {
//				a.setAvatarUrl(jsonuser.getStr("avatar"));
//			}
			if(StrUtil.isNotBlank(jsonuser.getStr("userEmail"))) {
				a.setEmail(jsonuser.getStr("userEmail"));
			}
			a.setName(jsonuser.getStr("userNameCn"));
			a.setGender(StrUtil.equals(jsonuser.getStr("sex"), "F")?2:1);
			a.setPhone(jsonuser.getStr("mobileNumber"));
			a.setAddTime(LocalDateTime.now());
			a.setAccountId(ua.getId());
			a.setUserType(Const.UserType.admin);
			a.save();
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
}
